package servlet;

import bean.Fruit;
import dao.impI.FruitDaoImpI;
import service.FruitService;
import service.ImpI.FruitServiceImpI;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebInitParam;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
@WebServlet(urlPatterns = "/addServlet",loadOnStartup = 1,
    initParams = {
            @WebInitParam(name = "encoding",value = "utf-8")
    }
)
public class AddServlet extends HttpServlet {
    String encoding=null;

    @Override
    public void init(ServletConfig config) throws ServletException {
        encoding = config.getInitParameter("encoding");
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding(encoding);
        Fruit fruit = new Fruit();
        fruit.setFname(req.getParameter("fname"));
        fruit.setFprice(Double.parseDouble(req.getParameter("fprice")));
        fruit.setDate(req.getParameter("fdate"));
        fruit.setFweight(Double.parseDouble(req.getParameter("fweight")));
        System.out.println(fruit);
        //FruitDaoImpI fruitDaoImpI = new FruitDaoImpI();
        //boolean flag = fruitDaoImpI.addFruit(fruit);
        FruitServiceImpI fruitServiceImpI = new FruitServiceImpI();
        boolean flag = fruitServiceImpI.addFruit(fruit);
        System.out.println(flag);
        if (flag){
            req.getRequestDispatcher("/productListServlet").forward(req,resp);
        }else {
            req.getRequestDispatcher("/exception.jsp").forward(req,resp);
        }

    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        this.doPost(req, resp);
    }




}
